package com.org.dao.impl;

import com.org.dao.UserDao;
import com.org.javabean.ShopInfo;
import com.org.javabean.UserInfo;
import com.org.util.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserDaoImp implements UserDao {

    @Override
    public List<UserInfo> selectAllUsers() {

        ResultSet rs = JDBCUtil.select("select *from userinfo",null);

        List<UserInfo> userInfos = new ArrayList<>();

        try {
            while (rs.next()){
                UserInfo userInfo = new UserInfo();
                userInfo.setUserId(rs.getInt("userid"));
                userInfo.setUserName(rs.getString("userName"));
                userInfo.setUserPW(rs.getString("userPsw"));
                userInfos.add(userInfo);

            }
        }catch (SQLException e){

        }


        return userInfos;
    }

    @Override
    public int getRegisterResult(String userName , String userPsw , String userTx) {
        String sql ="insert into user values(default,"+"'" +userName +"',"+"'" + userPsw +"',"+"'"+ userTx+"')";
        System.out.println(sql);
        Object[] obj = null;
        boolean falg = JDBCUtil.insertOrUpdateOrDelete( sql, obj);
        JDBCUtil.closeConn();
        return falg==true?1:0;
    }

    @Override
    public int getLoginResult(String userName, String userPsw) {
        String sql = "select *from user where userName = ? and userPW = ?";
        System.out.println(sql);
        Object[] obj = new Object[]{userName,userPsw};

        ResultSet rs = JDBCUtil.select(sql,obj);
        try {
            if(rs.next()){
                JDBCUtil.closeConn();

return 1;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        JDBCUtil.closeConn();
        return 0;
    }

    @Override
    public String getImgResult(String userName) {
        String sql = "select *from user where userName = ?";
        Object[] obj = new Object[]{userName};
        ResultSet rs = JDBCUtil.select(sql,obj);
        String filename = null;
        try {
            if(rs.next()){
filename = rs.getString("userTx");
             }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return filename;
    }

    @Override
    public List<ShopInfo> getShopList() {
        List<ShopInfo> list = new ArrayList<ShopInfo>();
        ResultSet rs = JDBCUtil.select("select *from img",null);
        try {
            while(rs.next()){
                int id = rs.getInt("id");
                String filename = "upload/"+ rs.getString("filename");
                String shopName = rs.getString("shopName");
                Double price = rs.getDouble("price");
list.add(new ShopInfo(id,filename,shopName,price));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return  list;
    }

    @Override
    public ShopInfo getShopInfo(int id) {
ShopInfo shopInfo = null;
Object[] obj = new Object[]{id};
        ResultSet rs = JDBCUtil.select("select *from img where id = ?",obj);
        try {
            if(rs.next()){
                String filename = "upload/"+ rs.getString("filename");
                String shopName = rs.getString("shopName");
                Double price = rs.getDouble("price");
               shopInfo =  new ShopInfo(id,filename,shopName,price);
            }
        }catch (SQLException e){
            e.printStackTrace();
        }
        return shopInfo;
    }


}
